package cn.lan.generator.service.impl;

import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.CreatePomXml;
import cn.lan.generator.utils.ConfigUtil;
import cn.lan.generator.utils.Constant;
import cn.lan.generator.utils.FileUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lanping
 * @version 1.0
 * @date 2019-07-06
 **/
public class CreatePomXmlFileImpl implements CreatePomXml {

    @Override
    public void writeFile(ProjInfo projInfo) throws Exception {

        String basePackage = projInfo.getPackageName();
        String srcFilePath = FileUtil.getResourceRootDir(projInfo.isDebugModel())+Constant.TEMPLATE_SERVICE_POM_PATH;
        String destFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getService(),
                null, "pom.xml");
        String commonPackageName = ConfigUtil.getCommonPackage(projInfo.getPackageName());
        String modulePackageName = ConfigUtil.getModulePackage(projInfo);

        Map<String,String> map = new HashMap<String,String>(){
            {
                put("filePackageName","pom.xml");
                put("basePackageName",basePackage);
                put("projectName",projInfo.getProjectName());
                put("projectSimpleName",projInfo.getProjectSimpleName());
                put("projectArtifactId","${project.artifactId}");
                put("javaVersion","${java.version}");
                put("commonPackageName", commonPackageName);
                put("commonModuleName", Module.getModule(projInfo).getCommon());
                put("modulePackageName", modulePackageName);
                put("serviceModuleName", Module.getModule(projInfo).getService());
                put("moduleName", Module.getModule(projInfo).getService());
                put("moduleSimpleName",projInfo.getModuleName());
            }
        };
        FileUtil.copyFile(projInfo,srcFilePath, destFilePath, map);
    }
}
